from general_utilities import *
import os

def make_new_highscore_list(filename):
  # return 1 if successful
  scores = [100,90,80,70,60,50,40,30,20,10] 
  names = ['AAA','BBB','CCC','DDD','EEE','FFF','GGG','HHH','III','JJJ']
  survived = [1,1,1,1,1,1,1,1,1,1]
  dump_highscores(filename,scores,names,survived)  

def get_highscore_list(filename):
  # return ([score],[name],[survived]) or ([],[],[])
  if not(os.path.exists(filename)):
    outcome = make_new_highscore_list(filename)
    if outcome==0:
      return ([],[],[])
  file = open(filename,'r')
  count = 0
  scores = []
  names = []
  survived = []
  for line in file:
    fields = line.split('\t')
    scores.append(float(fields[1]))
    names.append(fields[0])
    survived.append(int(fields[2]))
  file.close()
  return (scores,names,survived)

def get_list_add_highscore(filename,name,score,survived,dump_lowest):
  # return ([score],[name],[surviveds])
  (scores,names,surviveds) = get_highscore_list(filename)
  if score<min(scores):
    scores.append(score)
    names.append(name)
    surviveds.append(survived)
  elif score>max(scores):
    scores = [score] + scores
    names = [name] + names
    surviveds = [survived] + surviveds
  else:
    for i in range(1,len(scores)):
      if score>scores[i]:
        scores = scores[0:i] + [score] + scores[i:len(scores)]
        names = names[0:i] + [name] + names[i:len(names)]
        surviveds = surviveds[0:i] + [survived] + surviveds[i:len(surviveds)]
  if dump_lowest:
    scores = scores[0:len(scores)-1]
    names = names[0:len(names)-1]
    surviveds = surviveds[0:len(surviveds)-1]
  return (scores,names,surviveds)  

def dump_highscores(filename,scores,names,surviveds):
  # return 1 if successful
  try:
    file = open(filename,'w')
  except:
    return 0
  for i in range(len(scores)):
    file.write(names[i]+'\t'+str(scores[i])+'\t'+str(surviveds[i])+'\n')
  file.close()
  return 1  

highscores_available_for_epitaphs = []
scores_names_surviveds = get_highscore_list('save/highscore.lst')
for i in range(len(scores_names_surviveds[0])):
  if scores_names_surviveds[2][i]==0:
    if not (scores_names_surviveds[1][i] in highscores_available_for_epitaphs):
      highscores_available_for_epitaphs.append(scores_names_surviveds[1][i])
highscores_used_for_epitaphs = []

def get_epitaph():
  if len(highscores_available_for_epitaphs) > len(highscores_used_for_epitaphs):
    while 1:
      name = random.choice(highscores_available_for_epitaphs)
      if not (name in highscores_used_for_epitaphs):
        highscores_used_for_epitaphs.append(name)
        break
    return random.choice(("Here lies " + name + ".","R.I.P. " + name + ".","The bones of " + name + " rest here.","Alas for " + name + ", who is no more.","The remains of " + name + " were gathered up and dumped at this spot.")) 
  else:
    return random.choice(("Remember friend as you walk by,\nAs you are now so once was I,\nAs I am now you will surely be,\nPrepare thyself to follow me.",
                          "Ukka desecrated this grave, 137 B.P.",
                          "Do Not Disturb",
                          "Tomb of the Unknown Hero",
                          "Warning - contains Assorted Body Parts",
                          "Here lie I Martin Elginbrodde.\nHave mercy on my soul Lord God\nAs I would do were I Lord God\nAnd ye were Martin Elginbrodde",
                          "Toasted by the Firedrake, 135 B.P."))
